package com.somlsis.activity

import android.graphics.BitmapFactory
import android.os.Bundle
import android.os.PersistableBundle
import android.util.Log
import android.view.LayoutInflater
import com.amap.api.maps2d.AMap
import com.amap.api.maps2d.model.BitmapDescriptorFactory
import com.amap.api.maps2d.model.MyLocationStyle
import com.base.BaseActivity
import com.base.DeviceCenter
import com.event.StoreEvent
import com.net.HttpResponse
import com.somlsis.R
import com.somlsis.bean.Motion
import com.somlsis.bean.MotionData
import com.somlsis.bean.UserLatLng
import com.somlsis.databinding.ActivityRunningMapBinding
import com.somlsis.dialog.MotionDialog
import com.utils.GsonUtil
import com.utils.StringUtils
import org.greenrobot.eventbus.EventBus
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response


class RunningMapActivity : BaseActivity<ActivityRunningMapBinding>() {
    private var aMap: AMap? = null
    override fun getViewBinding(): ActivityRunningMapBinding {
        return ActivityRunningMapBinding.inflate(LayoutInflater.from(this))
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding.mapView.onCreate(savedInstanceState)

    }

    override fun createView() {
        createLocation()
        deviceCenter.setOnStrideListener(object : DeviceCenter.OnStrideListener {
            override fun onStride(
                isRun: Boolean,
                speed: String,
                frequency: Float,
                stride: Float,
                distance: Float,
                motionTime: Long,
                motionList: ArrayList<Motion>
            ) {
                binding.stepSize.text = stride.toString()
                binding.strideFrequency.text = frequency.toString()
                binding.speedAllocation.text = speed
                binding.distance.text = distance.toString()
                binding.exerciseTime.text = StringUtils.convertMillisToHMS(motionTime)
            }

        })
        binding.stopBt.setOnLongClickCircleViewListener {
            val motionDialog = MotionDialog(object : MotionDialog.OnMotionButtonClickListener {
                override fun onConfirmClick() {
                    val motionList = deviceCenter.stopRunning()
                    val latLngs = deviceCenter.getLatLngs()
                    val toJson = GsonUtil.toJson(MotionData(motionList, latLngs))
                    toJson?.let {
                        dataCenter.store(toJson, object : Callback<HttpResponse<String>> {
                            override fun onResponse(
                                call: Call<HttpResponse<String>>,
                                response: Response<HttpResponse<String>>
                            ) {
                                EventBus.getDefault().post(StoreEvent())
                                finish()
                            }

                            override fun onFailure(call: Call<HttpResponse<String>>, t: Throwable) {

                            }

                        })
                    }

                }

                override fun onCancelClick() {
                }

            })
            motionDialog.show(supportFragmentManager, "MotionDialog")
        }
    }

    private fun createLocation() {
        aMap = binding.mapView.map
        val style = MyLocationStyle()
        style.interval(30000)
        style.myLocationType(MyLocationStyle.LOCATION_TYPE_FOLLOW)
        val bitmap = BitmapFactory.decodeResource(resources, R.mipmap.icon_start_nor_60)
        val descriptor = BitmapDescriptorFactory.fromBitmap(bitmap)
        style.myLocationIcon(descriptor)
        style.showMyLocation(true)

        aMap?.let {
            it.uiSettings.setAllGesturesEnabled(false)
            it.setMyLocationStyle(style)
            it.setOnMyLocationChangeListener { location ->
                Log.d("LocationChange", "Location:${location.toString()}")

                deviceCenter.addLatLng(UserLatLng(location.latitude, location.longitude))
            }
            it.isMyLocationEnabled = true
        }
    }


    override fun onSaveInstanceState(outState: Bundle, outPersistentState: PersistableBundle) {
        super.onSaveInstanceState(outState, outPersistentState)
        binding.mapView.onSaveInstanceState(outState)
    }


    override fun onResume() {
        super.onResume()
        binding.mapView.onResume()
    }

    override fun onPause() {
        super.onPause()
        binding.mapView.onPause()
    }

    override fun onDestroy() {
        super.onDestroy()
        binding.mapView.onDestroy()

    }

}


